package cn.faifai.tagapi.service.BackgroundServices;

import cn.faifai.tagapi.service.BackgroundServices.FileWatcher.FileWatcherFactory;
import cn.faifai.tagapi.service.BackgroundServices.FileWatcher.FileWatcherService;
import cn.faifai.tagapi.utils.recycleBinPath.RecycleBinPathFetcher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 回收站文件夹监控启动服务
 */
@Slf4j
@Component
public class FileMonitoringStartsService implements CommandLineRunner {
    @Autowired
    private FileWatcherFactory fileWatcherFactory;
    @Autowired
    private RecycleBinPathFetcher recycleBinPathFetcher;
    @Override
    public void run(String... args) throws Exception {
        FileWatcherService fileWatcher = fileWatcherFactory.getFileWatcher();
        // 获取回收站链接
        List<String> recycleBinPath = recycleBinPathFetcher.getRecycleBinPath();
//        fileWatcher.start(Paths.get("D:\\$RECYCLE.BIN\\S-1-5-21-463390504-2282240019-982843704-1001"));
        ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
        recycleBinPath.forEach(item->executor.submit(()-> {
            try {
                log.info("文件监控启动成功: {}", item);
                fileWatcher.start(Paths.get(item));
            } catch (IOException e) {
                log.error("文件监控启动失败: {}", item);
                log.error("文件监控启动失败: {}", e);
            }
        }));
        log.info("回收站文件夹监控启动服务启动成功");
    }
}
